#include "aky_program/aky_com_include.h"

static void my_event_cb(lv_obj_t * obj, lv_event_t event);


static lv_obj_t *label_user1;          //
static lv_obj_t *label_user2;          //
static lv_obj_t *label_user3;          //
uint8_t flag_user1_ok = 0;
uint8_t flag_user2_ok = 0;
uint8_t flag_user3_ok = 0;

/*
 * 界面回调函数
 */
static void screen003_on_timer(void *user_data)
{
    /* 界面定时器回调函数 */
    screen_flush_pro();
}

static void screen003_start(synwit_id_t screen_id)
{
    /* 在界面被显示给用户前，这个接口会被调用。 */
    flag_user1_ok = 0;
    flag_user2_ok = 0;
    flag_user3_ok = 0;
    
    lv_font_t* font_temp = synwit_ui_font_load("S:spi:wqyMicroHeiMono_40.hmf", false);

    label_user1 = synwit_ui_find_lv_obj(WIDGET_LABEL9);
    label_user2 = synwit_ui_find_lv_obj(WIDGET_LABEL8);
    label_user3 = synwit_ui_find_lv_obj(WIDGET_LABEL7);

    lv_obj_set_style_local_text_font(label_user1,LV_LABEL_PART_MAIN,LV_STATE_DEFAULT,font_temp);
    lv_obj_set_style_local_text_font(label_user2,LV_LABEL_PART_MAIN,LV_STATE_DEFAULT,font_temp);
    lv_obj_set_style_local_text_font(label_user3,LV_LABEL_PART_MAIN,LV_STATE_DEFAULT,font_temp);

    lv_obj_set_click(label_user1, true);
    lv_obj_set_click(label_user2, true);
    lv_obj_set_click(label_user3, true);
    lv_obj_set_event_cb(label_user1, my_event_cb);   /*Assign an event callback*/
    lv_obj_set_event_cb(label_user2, my_event_cb);   /*Assign an event callback*/
    lv_obj_set_event_cb(label_user3, my_event_cb);   /*Assign an event callback*/


    g_s_mem_index = flash_get_mem_index_data(USER_1,&g_e_flash_err,MAIN_ADDR);
    if(g_s_mem_index.flag_is_user_info_valid == 1)
    {
        lv_label_set_text_fmt(label_user1,"%s",g_s_mem_index.user_name_buf);
        flag_user1_ok = 1;
    }
    else
    {
        lv_label_set_text_fmt(label_user1,"新增用户");
        flag_user1_ok = 0;
    }


    g_s_mem_index = flash_get_mem_index_data(USER_2,&g_e_flash_err,MAIN_ADDR);
    if(g_s_mem_index.flag_is_user_info_valid == 1)
    {
        lv_label_set_text_fmt(label_user2,"%s",g_s_mem_index.user_name_buf);
        flag_user2_ok = 1;
    }
    else
    {
        lv_label_set_text_fmt(label_user2,"新增用户");
        flag_user2_ok = 0;
    }


    g_s_mem_index = flash_get_mem_index_data(USER_3,&g_e_flash_err,MAIN_ADDR);
    if(g_s_mem_index.flag_is_user_info_valid == 1)
    {
        lv_label_set_text_fmt(label_user3,"%s",g_s_mem_index.user_name_buf);
        flag_user3_ok = 1;
    }
    else
    {
        lv_label_set_text_fmt(label_user3,"新增用户");
        flag_user3_ok = 0;
    }
    // 打开下面的注释可以为本界面开启一个每100ms触发一次的定时器
    screen_init();

    synwit_ui_start_scr_timer(10, NULL);
}

static void screen003_stop(synwit_id_t screen_id)
{
    /* 准备切换到其它界面前，这个接口会被调用。 */
    screen_stop();

}

static int screen003_favorite_stop_policy()
{
    /* 函数返回值表示本界面结束时，希望采用的资源管理策略 */

    /* 资源管理策略(主要影响本界面再次进入时的加载速度):
     *    0: 系统推荐，释放当前界面引用的资源，保留控件及控件状态。可获得平衡的界面切换性能和内存消耗
     *    1: 释放当前界面引用的资源，并删除所有控件。
     *    2: 保留当前界面的所有资源及控件。可以获得最佳的界面切换性能，但内存消耗较大
     */
    return 0;
}


/*
 * 界面注册对象
 */
synwit_screen_callback_t screen003_cb_obj = {
    .favorite_stop_policy = screen003_favorite_stop_policy,
    .on_screen_start = screen003_start,
    .on_screen_stop = screen003_stop,
    .on_screen_timer = screen003_on_timer,
};
static void my_event_cb(lv_obj_t * obj, lv_event_t event)
{
    off_time_clr();
    if (obj == label_user1)
    {
        if (event == LV_EVENT_CLICKED)
        {
            g_e_user = USER_1;
            if (flag_user1_ok == 1)
            {
                synwit_ui_load_screen(SCREEN005);
            }
            else
            {
                synwit_ui_load_screen(SCREEN004);
            }
        }
    }
    if (obj == label_user2)
    {
        if (event == LV_EVENT_CLICKED)
        {
            g_e_user = USER_2;
            if (flag_user2_ok == 1)
            {
                synwit_ui_load_screen(SCREEN005);
            }
            else
            {
                synwit_ui_load_screen(SCREEN004);
            }
        }
    }
    if (obj == label_user3)
    {
        g_e_user = USER_3;
        printf("label_user3 event = %d\r\n",event);
        if (event == LV_EVENT_CLICKED)
        {
            if (flag_user3_ok == 1)
            {
                synwit_ui_load_screen(SCREEN005);
            }
            else
            {
                synwit_ui_load_screen(SCREEN004);
            }
        }
        else if (event == LV_EVENT_LONG_PRESSED)
        {
            if (flag_user3_ok == 1)
            {
               synwit_ui_load_screen(SCREEN004);
            }
        }
        
    }
}
